package com.zengqingfa.springboot.redis.rest;

import com.alibaba.fastjson.JSON;
import com.zengqingfa.springboot.redis.delay.entity.Order;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

import static com.zengqingfa.springboot.redis.constants.RedisCacheKeyConstants.*;

/**
 * @author zengqingfa
 * @className RedisKeyExpirationController
 * @description
 * @create 2022/9/24 19:55
 */
@RestController
@RequestMapping("/redis/expire")
@Slf4j
public class RedisKeyExpirationController {

    @Resource
    private RedisTemplate redisTemplate;

    @GetMapping("/message")
    public Boolean message( @RequestParam("id") Long id) {
        String key = EXPIRE_KEY_MESSAGE + id;
        redisTemplate.opsForValue().set(key, DEFAULT_EXPIRE_VALUE, TIME, TimeUnit.SECONDS);
        log.info("存入redis成功，key：{}，value：{}", key, DEFAULT_EXPIRE_VALUE);
        return true;
    }

    @GetMapping("/order")
    public Boolean order(@RequestParam("id") Long id) {
        String key = EXPIRE_KEY_MESSAGE + id;
        redisTemplate.opsForValue().set(key, DEFAULT_EXPIRE_VALUE, TIME, TimeUnit.SECONDS);
        log.info("存入redis成功，key：{}，value：{}", key, DEFAULT_EXPIRE_VALUE);
        return true;

    }
}